public class Solution {
    class ListNode{
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode partition(ListNode pHead, int x) {
        ListNode cur = pHead;
        ListNode less = new ListNode(0);
        ListNode lessHead = less;

        ListNode more = new ListNode(0);
        ListNode moreHead = more;

        while(cur != null){
            if(cur.val < x){
                less.next = cur;
                less = less.next;
            }else {
                more.next = cur;
                more = more.next;
            }
            cur = cur.next;
        }
        more.next = null;
        less.next = moreHead.next;
        return lessHead.next;
    }
}
